<template>
	<view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">电话：</text>
			<uni-easyinput :disabled="true"  type="number" v-model="tel" placeholder="请输入家长联系方式" @blur="chaxuntel" />
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">姓名：</text>
			<uni-easyinput :disabled="true" type="text" v-model="name" placeholder="请输入学生姓名" />
		</view>
		
			
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">报名日期：</text>
			<picker mode="date" :value="baomingdate" :start="startDate" :end="endDate" @change="bindDateChange">
				<uni-easyinput type="text" v-model="baomingdate" placeholder="请输入报名日期" />
			</picker>
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">截止日期：</text>
			<picker mode="date" :value="jiezhidate" :start="startDate" :end="endDate" @change="bindDateChange1">
				<uni-easyinput type="text" v-model="jiezhidate" placeholder="请输入截止日期" />
			</picker>
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">报名费用：</text>
			<uni-easyinput type="number" v-model="money1" placeholder="请输入报名所交费用" />
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">收费方式：</text>
			<uni-easyinput type="text" v-model="shoufeifangshi" placeholder="请输入收费的方式" />
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">课程数量：</text>
			<uni-easyinput type="number" v-model="kechengshu" placeholder="请输入报名课程的数量" />
		</view>
		<view style="display: flex;flex-direction: row;align-items: center;width: 80%;margin: 20rpx auto;">
			<text style="color: blue;font-weight: 500;">已核销数量：</text>
			<uni-easyinput type="number" v-model="yihexiaoshu" placeholder="请输入已上课程数量" />
		</view>		
		<view @click="tijiao"
			style="display: flex;background-color: blue;width: 40%; border-radius: 20rpx;margin: 60rpx auto;color: white;justify-content: center;align-items: center;padding: 20rpx 0;">
			提交</view>
	</view>
</template>

<script>
	import getDateTime from '@/common/getdateTime.js';
	import toast from '../../uni_modules/uview-ui/libs/config/props/toast';
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				detail: '', //修改明细记录
				name: '',
				age: '',
				kecheng: '',
				tel: '',
				baomingdate: '',
				jiezhidate: '',
				kechengshu: null,
				yihexiaoshu: 0,
				money1: undefined,
				shoufeifangshi: '',
				xiaoqulist: [], //从数据库中获取校区LIST
				infor: [], //获取要修改学生的信息
			}
		},
		async onLoad() {
			let meiyi_name_tel=await uni.getStorageSync('meiyi_name_tel')
			this.jingshouren=meiyi_name_tel.name+meiyi_name_tel.mobile
			this.meiyi_userinfo=await uniCloud.getCurrentUserInfo()
			this.infor = await uni.getStorageSync('aoyashenginfor')
			console.log('55',this.infor);
			this.duqushuju()
			this.get_xiaoqu()
			this.baomingdate = getDateTime.dateTimeStr('y-m-d'); // y:年 m:月 d:日 h:时 i:分 s:秒 中间的分割符号可更改
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			get_xiaoqu() {
				uniCloud.callFunction({
					name: "funshare",
					data: {
						leixing: 1, //类型为1时，获取我管理的校区
						guanlilaoshiid: this.meiyi_userinfo.uid,
					}
				}).then(res => {
					// res 为数据库查询结果
					this.xiaoqulist1 = res.result.data
					this.xiaoqulist1.map(item => {
						this.xiaoqulist.push(item.name)
					})
				})
			},
			//读取学生的基本数据
			duqushuju() {
				this.tel = this.infor.id[0].mobile
				this.name = this.infor.id[0].name
				this.age = this.infor.id[0].age
			},
			//获取日期选择期的起始时间段
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year ;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			//报名时间
			bindDateChange: function(e) {
				this.baomingdate = e.detail.value
			},
			//截止日期
			bindDateChange1: function(e) {
				this.jiezhidate = e.detail.value
			},
			// 添加报课明记录,添加账务流水，修改上级课程续费状态
			tijiao() {
				uni.showLoading({title: '提交中……',mask:true});
				// 一、baokelist添加一条新数据，二、zhangwuliushui添加一条新数据，三、上一个课程在baokelist改xufeifrom信息，四、xiugaishuju添加一条
				if(this.tel==''||this.name==''||this.baomingdate==''||this.jiezhidate==''||this.money1==''||this.money1==undefined||this.shoufeifangshi==''||this.kechengshu==''||this.kechengshu==undefined||this.yihexiaoshu<0){
					uni.hideLoading()
					return uni.showToast({
						title:'请填完所有选项，再继续',
						icon:'none',
						duration:3000
					})
				}
				if(this.kechengshu<1 || parseInt(this.kechengshu)!=this.kechengshu){
					uni.hideLoading()
					uni.showToast({
						title: '课程数量必须为正整数！',
						icon: 'none',
						mask: true
					})
					return 
				}
				if(parseInt(this.kechengshu)<=parseInt(this.yihexiaoshu)){
					uni.hideLoading()
					uni.showToast({
						title: '核销的数量需小于课程总数量！',
						icon: 'none',
						mask: true
					})
					return 
				}
				uniCloud.callFunction({
					name:"xinhuiyuan",
					data:{
						leixing:3,//
						leibie:"续费",
						jingshouren:this.jingshouren,
						baomingdate:this.baomingdate,
						jiezhidate:this.jiezhidate,
						kechengshu:parseInt(this.kechengshu),
						money1:parseInt(this.money1),
						yihexiaoshu:parseInt(this.yihexiaoshu),
						shoufeifangshi:this.shoufeifangshi,
						id:this.infor.id[0]._id,
						xiaoqu:this.infor.xiaoqu,
						yikefeiyong:parseInt(parseInt(this.money1) / parseInt(this.kechengshu) * 100) / 100,
						kecheng:this.infor.kecheng,
						zhuangtai:this.infor.zhuangtai==1?2:1,
						xufeifrom:this.infor._id,
						detail: this.name+this.tel + "续费，课程名称：" + this.infor.kecheng + "，课程数量："+this.kechengshu + "节",
						riqi:getDateTime.dateTimeStr('y-m-d h:i:s'),
						kechengid:this.infor.kechengid
					}
				}).then(res=>{
					uni.showToast({
						title: '续费成功',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					uni.reLaunch({
						url:'/pages/xueshenginfor/xueshenginfor'
					})
				})
				
			},
			
		}
	}
</script>

<style>

</style>